package com.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.entity.Content;
import com.hzit.entity.ContentCategory;
import com.hzit.service.ContentCategoryService;
import com.hzit.service.ContentService;
import com.hzit.mapper.ContentMapper;
import com.cwy.hzit.utils.PageResult;
import com.hzit.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cwy
 * @since 2025-01-15
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content>
    implements ContentService {
    @Autowired
    private ContentCategoryService contentCategoryService;
    
    // 3. 条件带分页查询
    @Override
    public PageResult<Content> search(Integer page, Integer pageSize, ContentVO vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Content> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getTitle()), "e_name", vo.getTitle());
        wrapper.eq(vo.getCategoryId() != null, "e_depart", vo.getCategoryId());
        // 1.2 开始分页查询
        Page<Content> page1 = this.page(new Page<>(page, pageSize), wrapper);
        // 1.3 处理员工所在部门名称
        page1.getRecords().forEach(content -> {
            ContentCategory contentCategory = contentCategoryService.getById(content.getCategoryId());
            content.setName(contentCategory.getName());
        });
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    // 添加学生
    @Override
    public boolean add(Content content) {
        return this.save(content) ;
    }
}




